package com.hundsun.cprs.yyt.service.form.service.impl;

import java.util.List;

import com.hundsun.cprs.yyt.service.form.domain.Form;
import com.hundsun.cprs.yyt.service.form.domain.FormAttrValues;
import com.hundsun.cprs.yyt.service.form.dto.FormInfo;
import com.hundsun.cprs.yyt.service.form.service.FormAttrValuesService;
import com.hundsun.cprs.yyt.service.form.service.FormFacadeService;
import com.hundsun.cprs.yyt.service.form.service.FormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hundsun.cprs.yyt.common.base.BaseService;
import com.hundsun.cprs.yyt.service.form.domain.Form;
import com.hundsun.cprs.yyt.service.form.domain.FormAttrValues;
import com.hundsun.cprs.yyt.service.form.dto.FormInfo;
import com.hundsun.cprs.yyt.service.form.service.FormAttrValuesService;
import com.hundsun.cprs.yyt.service.form.service.FormFacadeService;
import com.hundsun.cprs.yyt.service.form.service.FormService;

@Service
public class FormFacadeServiceImpl extends BaseService implements FormFacadeService {
    
    @Autowired
    private FormService formService;
    
    @Autowired
    private FormAttrValuesService formAttrValuesService;
    
    @Override
    public FormInfo getFormInfoByCode(String formCode) {
        return formService.getFormInfoByFormCode(formCode);
    }

    @Override
    public List<FormAttrValues> selectByFormCodeAndObjectId(String formCode, Long objectId) {
        FormInfo form = formService.getFormInfoByFormCode(formCode);
        if (form == null) {
            return null;
        }
        return formAttrValuesService.selectByFormIdAndObjectId(form.getId(), objectId);
    }

    @Override
    public void createFormAttrValues(List<FormAttrValues> attrValuesList) {
        createFormAttrValues(attrValuesList,null);
    }

    @Override
    public void createFormAttrValues(List<FormAttrValues> attrValuesList, Long objectId) {
        if (objectId != null) {
            for(FormAttrValues item : attrValuesList){
                item.setObjectId(objectId);
            }
        }
        formAttrValuesService.createFormAttrValues(attrValuesList);
    }

    @Override
    public void updateFormAttrValues(List<FormAttrValues> attrValuesList) {
        formAttrValuesService.updateFormAttrValues(attrValuesList);
    }

    @Override
    public void deleteFormAttrValues(String formCode, Long objectId) {
        Form form = formService.getFormByFormCode(formCode);
        if (form == null) {
            return;
        }
        formAttrValuesService.deleteFormAttrValues(form.getId(), objectId);
    }

}
